﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BuildOptimizer
{
    class BoCommandStartFromRoot : BoCommand
    {
        private const commandID _commandID = commandID.startFromRoot;

        public BoCommandStartFromRoot()
        {
        }

        public override void execute(BoGoal goal, BoDecisionNode node)
        {
            node.gameState.setToStartState();
            initUnlockedCommands(node);
            initRequiredCommands(goal, node);

            checkIfMoreRequired(goal, node);
            BoTask t = new BoTask(5, delegate(BoDecisionNode n)
            {
                for (int i = 0; i < 6; i++)
                {
                    n.gameState.getHatcheryAtResources(0).makeDroneMineMinerals();
                }
            });
            node.gameState.addTask(t);
        }

        private void initUnlockedCommands(BoDecisionNode node)
        {
            node.isCommandUnlocked[(int)commandID.buildEvolutionChamber] = true;
            node.isCommandUnlocked[(int)commandID.buildExtractor] = true;
            node.isCommandUnlocked[(int)commandID.buildHatcheryAtResourcePatch] = true;
            node.isCommandUnlocked[(int)commandID.buildHatcheryInBase] = true;
            node.isCommandUnlocked[(int)commandID.buildSpawningPool] = true;

            node.isCommandUnlocked[(int)commandID.buildDrone] = true;
            node.isCommandUnlocked[(int)commandID.buildOverlord] = true;

            node.isCommandUnlocked[(int)commandID.startFromRoot] = true;
        }
        private void initRequiredCommands(BoGoal goal, BoDecisionNode node)
        {
            node.isCommandRequired[(int)commandID.buildBanelingNest] = BoCommandBuildBanelingNest.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildEvolutionChamber] = BoCommandBuildEvolutionChamber.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildExtractor] = BoCommandBuildExtractor.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildGreaterSpire] = BoCommandBuildGreaterSpire.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildHatcheryAtResourcePatch] = BoCommandBuildHatcheryAtResourcePatch.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildHatcheryInBase] = BoCommandBuildHatcheryInBase.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildHive] = BoCommandBuildHive.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildHydraliskDen] = BoCommandBuildHydraliskDen.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildInfestationPit] = BoCommandBuildInfestationPit.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildLair] = BoCommandBuildLair.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildNydusNetwork] = BoCommandBuildNydusNetwork.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildNydusWorm] = BoCommandBuildNydusWorm.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildRoachWarren] = BoCommandBuildRoachWarren.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildSpawningPool] = BoCommandBuildSpawningPool.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildSpineCrawler] = BoCommandBuildSpineCrawler.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildSpire] = BoCommandBuildSpire.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildSporeCrawler] = BoCommandBuildSporeCrawler.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildUltraliskCavern] = BoCommandBuildUltraliskCavern.checkIfMoreRequired(goal, node);

            node.isCommandRequired[(int)commandID.buildBaneling] = BoCommandBuildBaneling.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildBroodlord] = BoCommandBuildBroodlord.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildCorruptor] = BoCommandBuildCorruptor.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildDrone] = BoCommandBuildDrone.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildHydralisk] = BoCommandBuildHydralisk.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildInfestor] = BoCommandBuildInfestor.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildMutalisk] = BoCommandBuildMutalisk.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildOverlord] = BoCommandBuildOverlord.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildOverseer] = BoCommandBuildOverseer.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildQueen] = BoCommandBuildQueen.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildRoach] = BoCommandBuildRoach.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildUltralisk] = BoCommandBuildUltralisk.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.buildZergling] = BoCommandBuildZergling.checkIfMoreRequired(goal, node);

            node.isCommandRequired[(int)commandID.upgradeAdrenalGlands] = BoCommandUpgradeAdrenalGlands.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeBurrow] = BoCommandUpgradeBurrow.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeCentrifugalHooks] = BoCommandUpgradeCentrifugalHooks.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeChitinousPlating] = BoCommandUpgradeChitinousPlating.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeFlyerArmor1] = BoCommandUpgradeFlyerArmor1.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeFlyerArmor2] = BoCommandUpgradeFlyerArmor2.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeFlyerArmor3] = BoCommandUpgradeFlyerArmor3.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeFlyerAttacks1] = BoCommandUpgradeFlyerAttacks1.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeFlyerAttacks2] = BoCommandUpgradeFlyerAttacks2.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeFlyerAttacks3] = BoCommandUpgradeFlyerAttacks3.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeGlialReconstitution] = BoCommandUpgradeGlialReconstitution.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeGroovedSpines] = BoCommandUpgradeGroovedSpines.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeGroundArmor1] = BoCommandUpgradeGroundArmor1.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeGroundArmor2] = BoCommandUpgradeGroundArmor2.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeGroundArmor3] = BoCommandUpgradeGroundArmor3.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeMelee1] = BoCommandUpgradeMelee1.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeMelee2] = BoCommandUpgradeMelee2.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeMelee3] = BoCommandUpgradeMelee3.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeMetabolicBoost] = BoCommandUpgradeMetabolicBoost.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeMissile1] = BoCommandUpgradeMissile1.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeMissile2] = BoCommandUpgradeMissile2.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeMissile3] = BoCommandUpgradeMissile3.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeNeuralParasite] = BoCommandUpgradeNeuralParasite.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradePathogenGlands] = BoCommandUpgradePathogenGlands.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradePneumatizedCarapace] = BoCommandUpgradePneumatizedCarapace.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeTunnelingClaws] = BoCommandUpgradeTunnelingClaws.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.upgradeVentralSacs] = BoCommandUpgradeVentralSacs.checkIfMoreRequired(goal, node);

            node.isCommandRequired[(int)commandID.queenCreepTumor] = BoCommandQueenCreepTumor.checkIfMoreRequired(goal, node);
            node.isCommandRequired[(int)commandID.queenInject] = BoCommandQueenInject.checkIfMoreRequired(goal, node);

            node.isCommandRequired[(int)commandID.startFromRoot] = BoCommandStartFromRoot.checkIfMoreRequired(goal, node);
        }

        public static bool checkIfMoreRequired(BoGoal goal, BoDecisionNode node)
        {
            node.dismissCommand((int)commandID.startFromRoot);
            return false;
        }
        public static bool isAvailable(BoGameState gs)
        {
            return true;
        }
        public static bool isAffordable(BoGameState gs)
        {
            return true;
        }

        public override int getCommandCode()
        {
            int code = ((int)_commandID * 100);
            return code;
        }
        public override commandID getCommandID()
        {
            return _commandID;
        }
        public override string toString()
        {
            string str = "Start";
            return str;
        }
    }
}
